#!/bin/bash
#********************************************************************
# Author: zhangjunjie
# Date: 2022-05-23
# FileName： install_redis_for_centos.sh
#********************************************************************

## 先判断是否存在自定义的方法文件, 如果不存在, 则进行下载操作
SRC_DIR=/usr/local/src
REDIS_VERSION=redis-6.2.7
REDIS_PASSWORD=123456
INSTALL_DIR=/apps/redis

mkdir -p $SRC_DIR
cd $SRC_DIR
if [ ! -e self_function.sh ]; then
	yum install wget -y -q || { echo -e "\E[31;1m 安装软件包失败 \E[0m" ; exit; }

	echo -e "\E[33;1m 下载自定义方法中 \E[0m"
	wget https://gitee.com/gkdaxue/shell_script/raw/master/self_function.sh
	if [ ! -e self_function.sh ]; then 
		echo -e "\E[31;1m 下载自定义方法失败 \E[0m"
		exit;
	fi
fi

## 加载自定义方法
. ./self_function.sh
actions 'echo_tips' "加载自定义方法";

## 校验前提条件
check (){
	yum install gcc jemalloc-devel systemd-devel  -y  || { actions 'echo_error' "安装软件包失败，请检查网络配置"; exit; }
	if [ ! -e ${REDIS_VERSION}.tar.gz ]; then
		actions 'echo_tips' "软件包开始下载";
		wget http://download.redis.io/releases/${REDIS_VERSION}.tar.gz && { actions 'echo_success' "软件包下载成功"; } || { actions 'echo_error' "Redis 源码下载失败"; exit; }
	else
		actions "echo_success" "使用本地${REDIS_VERSION}安装包进行安装"	
	fi	
	id redis &> /dev/null && { actions "echo_tips" "Redis 用户已存在" ; } || { useradd -r -s /sbin/nologin redis ; actions "echo_success" "Redis 用户创建成功" ; }
}

## 安装
install (){
	tar xf ${REDIS_VERSION}.tar.gz
	cd ${REDIS_VERSION}
	make USE_SYSTEMD=yes -j 1 PREFIX=${INSTALL_DIR} install && actions "echo_tips" "Redis 编译安装完成" || { actions 'echo_error' "Redis 编译安装失败" ; exit ; }
	mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
	cp redis.conf  ${INSTALL_DIR}/etc/
	cp sentinel.conf ${INSTALL_DIR}/etc/redis-sentinel.conf

	sed -i -e '/^bind 127.0.0.1/c bind 0.0.0.0' -e '/^daemonize no/c daemonize yes'  -e "/# requirepass/a requirepass $REDIS_PASSWORD"  -e "/^dir .*/c dir ${INSTALL_DIR}/data/"  -e "/^logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log"  -e  "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf

	chown -R redis.redis ${INSTALL_DIR}
	cat >> /etc/profile.d/redis.sh <<EOF
PATH=$PATH:${INSTALL_DIR}/bin/
export PATH
EOF
	. /etc/profile.d/redis.sh

	cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF

	sysctl -p
	echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
	chmod +x /etc/rc.d/rc.local
	/etc/rc.d/rc.local
	cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF
	
## 生成哨兵配置文件
	cat > /usr/lib/systemd/system/redis-sentinel.service <<EOF
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-sentinel ${INSTALL_DIR}/etc/redis-sentinel.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF

	systemctl daemon-reload
	systemctl start redis &> /dev/null && actions 'echo_tips' "Redis 服务启动成功,Redis信息如下:" || { actions 'echo_error' "Redis 启动失败" ;exit; }
	redis-cli -a $REDIS_PASSWORD INFO Server 2> /dev/null
}

check 
install
